package map.data
{
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import map.data.BirthPoint;
	import map.data.Front;
	import map.data.Obstacle;
	import map.data.RadCell;
	import map.geometry.RadLine;

	public class Mapdata
	{
		public var bgurl:String;
		public var cells:Vector.<RadCell>;
		public var obstacles:Vector.<Obstacle>;
		public var fronts:Vector.<Front>;
		public var birthpoints:Vector.<BirthPoint>;
		public function Mapdata()
		{
			
		}
		
		/**
		 * 生成邻居数据 
		 * 
		 */		
		public function adapt():void
		{
			var cell:RadCell = cells[0];
			for each(cell in cells)
			{
				cell.neighbors = [];
				for each(var cell1:RadCell in cells)
				{
					if(cell != cell1 && cell.isNeighbor(cell1))
					{
						cell.neighbors.push(cells.indexOf(cell1));
					}
				}
			}
			for each(cell in cells)
			{
				var neighbors:Array = cell.neighbors.concat();
				for each(var cell1Index:int in neighbors)
				{
					cell1 = cells[cell1Index];
					var line:RadLine = new RadLine(cell1.center,cell.center);
					for each(var obs:Obstacle in obstacles)
					{
						if(line.intersect(obs.line))
						{
							cell.neighbors.splice(cell.neighbors.indexOf(cells.indexOf(cell1)),1);
						}
					}
				}
			}
		}
		
		public static function decode(obj:Object):Mapdata
		{
			var result:Mapdata = new Mapdata();
			result.bgurl = obj.bgurl;
			result.birthpoints = new Vector.<BirthPoint>();
			for each(var bp:Object in obj.birthpoints)
			{
				result.birthpoints.push(BirthPoint.decode(bp));
			}
			result.cells = new Vector.<RadCell>();
			for each(var cell:Object in obj.cells)
			{
				result.cells.push(RadCell.decode(cell));
			}
			result.fronts = new Vector.<Front>();
			for each(var front:Object in obj.fronts)
			{
				result.fronts.push(Front.decode(front));
			}
			result.obstacles = new Vector.<Obstacle>();
			for each(var obs:Object in obj.obstacles)
			{
				result.obstacles.push(Obstacle.decode(obs));
			}
			return result;
		}
	}
}